home *** CD-ROM | disk | FTP | other *** search
- ;unsigned short search_left(strg,sub_strg,start_pt);
- ; char *strg,*sub_strg;
- ; unsigned short start_pt;
-
- EXTRN _memory_model:byte
- EXTRN _error_code:byte
-
- _TEXT SEGMENT BYTE PUBLIC 'CODE'
- ASSUME CS:_TEXT
- PUBLIC _search_left
- _search_left proc near
- push bp ;
- mov bp,sp ;set up stack frame
- push di ;
- push si ;
- push ds ;save Turbo's DS
- cmp _memory_model,0 ;near or far?
- jle begin ;jump if near
- inc bp ;else add 2 to BP
- inc bp ;
- begin: cmp _memory_model,2 ;data near or far?
- jb L0 ;jump if near
- les di,dword ptr[bp+4] ;ES:DI pts to Strg
- lds si,dword ptr[bp+8] ;DS:SI pts to Substrg
- add bp,4 ;adjust stack pointer
- jmp short L00 ;jump ahead
- L0: mov ax,ds ;near case
- mov es,ax ;
- mov di,[bp+4] ;
- mov si,[bp+6] ;
- L00: mov bx,[bp+8] ;startpoint in BX
- inc bx ;count from 1
- push di ;calculate string length
- mov cx,0ffffh ;counter
- sub al,al ;look for ASCII 0
- cld ;direction forward
- repne scasb ;seek end
- not cx ;reverse and adjust
- dec cx ;length now in CX
- pop di ;restore Strg ptr
- mov bp,2 ;2 = Strg null
- jcxz L6 ;quit if null
- sub ah,ah ;
- push si ;get length of substring
- L1: cmp byte ptr [si],0 ;
- je L2 ;
- inc ah ;
- inc si ;
- jmp short L1 ;
- L2: pop si ;
- inc bp ;3 = Substrg null
- or ah,ah ;test substring length
- jz L6 ;quit if null
- add di,bx ;forward DI to startpoint
- dec di ;adjust
- mov al,[si] ;use char for SCAS below
- inc si ;start SCAS from 2nd char
- sub cl,ah ;strg len - substring len
- sub cx,bx ; - startpoint
- add cx,3 ;adjust
- dec bp ;2 = startpt out of range
- cmp cl,1 ;len-startpt big enough?
- jle L6 ;if not, quit routine
- dec bp ;1 = not found
- dec bx ;adjust BX for loop below
- cld ;direction flag forward
- L3: mov dx,cx ;store num chars to scan
- jcxz L5 ;in case Strg is 1 char
- repne scasb ;search the 1st char
- jcxz L5 ;jump ahead if not found
- sub dx,cx ;chars to scan - remainder
- add bx,dx ;add to startpoint
- push cx ;save num chars to scan
- mov cl,ah ;substring len in CX
- push si ;save string pointer
- push di ;save substring pointer
- repe cmpsb ;compare the substring
- pop di ;restore string pointer
- pop si ;restore substring pointer
- jcxz L4 ;substring found if CX=0
- pop cx ;set CX for next scan
- jmp short L3 ;not matching, try again
- L4: pop cx ;since pushed before CMPS
- mov bp,0 ;0 = no error
- jmp short L7 ;found! quit routine
- L5: mov bp,1 ;error code when not found
- L6: mov bx,1 ;return 0 when not found (DECed)
- L7: mov ax,bx ;place result for return
- dec ax ;count from zero
- pop ds ;restore DS
- mov bx,bp ;get error return value
- mov _error_code,bl ;set _error_code
- pop si ;
- pop di ;
- pop bp ;
- cmp _memory_model,0 ;quit
- jle quit ;
- db 0CBh ;RET far
- quit: ret ;RET near
- _search_left ENDP
- _TEXT ENDS
- END